package com.learn2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect     // 声明本类是一个切面描述类
@Component  // 声明本类接受Spring 容器的管理
public class LogAspect {

    // @Pointcut 声明一个切入点    http://sishuok.com/forum/posts/list/281.html
    @Pointcut("@annotation(com.learn2.Action)")
    public void annotationPointCut(){ }

    @After("annotationPointCut()")      // 在 annotationPointCut() 这个切入点执行定义好的方法
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("运行过后拦截");
        System.out.println("注解式拦截（LogAspect） " + action.name());
    }


    @Before("execution(* com.learn2.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("运行之前拦截");
        System.out.println("方法规则式拦截（LogAspect） " + method.getName());
    }

    @Around("execution(* com.learn2.AroundMethodService.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("@Around 环绕通知    start");
        proceedingJoinPoint.proceed();
        System.out.println("@Around 环绕通知    end");
    }
}
